package com.scorekeeper.objects;

import java.util.Map;

import com.scorekeeper.utils.DBProvider;
import com.scorekeeper.utils.Loader;

import android.os.Parcel;
import android.os.Parcelable;

public class Team implements Parcelable {
	public int _id;
	public String name;
	public String acro;
	public int gamesPlayed;
	public int wins;
	public int losses;
	public int points;
	public int divisionId;
	public Map<Long, Game> games = null;
	public Map<String, Extension> extensions = null;
	public int standingsPos = 0;	//default zero, only set when sorted by Loader.SortTeamsByPoints
	
	public Team() {
		this._id = -1;
	}
	
	public Division division() {
		return (new Loader()).loadDivision(divisionId);
	}
	
	public void loadGames() {
		games = (new Loader()).loadGames(_id);
	}
	
	public void loadExtensions() {
		extensions = (new Loader()).loadTeamExtensions(_id);
	}
	
	public int describeContents() { return hashCode(); }
	
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeLong(this._id);
		dest.writeString(this.name);
		dest.writeString(this.acro);
		dest.writeInt(this.gamesPlayed);
		dest.writeInt(this.wins);
		dest.writeInt(this.losses);
		dest.writeInt(this.points);
		dest.writeInt(this.divisionId);
	}
	
	public void loadFromParcel(Parcel loadFrom) {
		this._id = loadFrom.readInt();
		this.name = loadFrom.readString();
		this.acro = loadFrom.readString();
		this.gamesPlayed = loadFrom.readInt();
		this.wins = loadFrom.readInt();
		this.losses = loadFrom.readInt();
		this.points = loadFrom.readInt();
		this.divisionId = loadFrom.readInt();
	}
	
	public void save() {
		DBProvider db = new DBProvider();
		
		if (this._id != -1) {
			//update
			db.updateObject(DBProvider.Table.TEAM, this);
		} else {
			//insert
			db.saveObject(DBProvider.Table.TEAM, this);
		}
		
		db.close();
	}
}